package com.camel.system.exceptions;

import cn.dev33.satoken.exception.NotLoginException;
import com.camel.core.entity.Result;
import com.camel.core.utils.ResultUtil;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author camel
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 全局异常拦截
     * @param e
     * @param req
     * @param resp
     * @throws IOException
     */
    @ExceptionHandler
    public void handlerException(NotLoginException e, HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setStatus(401);
    }

    /**
     * 全局异常拦截
     * 方法参数找不到异常
     * @param e
     * @return
     */
    @ExceptionHandler
    public Result handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        e.printStackTrace();
        return ResultUtil.error(500, e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
    }
}
